<?php
class topic extends spController
{
	function __construct(){ // 公用
		parent::__construct(); // 这是必须的
		$this->tpl_title = "后台模块|文章部分";
	}
	
	function index(){ // 这里是首页
		$this->contents = "后台模块文章部分首页内容";
		$this->display("admin/topic_index.html");
	}
	
	function page(){ // 其他内容
		//$this->pchat();
		$this->contents = "后台模块文章部分其他内容";
		$this->display("admin/topic_page.html");
	}
	
	function upload() {
		header ( 'Content-Type: text/html; charset=UTF-8' );
		
		$inputName = 'filedata'; //表单文件域name
		$attachDir = 'uploads'; //上传文件保存路径，结尾不要带/
		$dirType = 1; //1:按天存入目录 2:按月存入目录 3:按扩展名存目录  建议使用按天存
		$maxAttachSize = 2097152; //最大上传大小，默认是2M
		$upExt = 'txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid'; //上传扩展名
		$msgType = 2; //返回上传参数的格式：1，只返回url，2，返回参数数组
		$immediate = isset ( $_GET ['immediate'] ) ? $_GET ['immediate'] : 0; //立即上传模式，仅为演示用
		ini_set ( 'date.timezone', 'Asia/Shanghai' ); //时区
		

		$err = "";
		$msg = "''";
		$tempPath = $attachDir . '/' . date ( "YmdHis" ) . mt_rand ( 10000, 99999 ) . '.tmp';
		$localName = '';
		
		if (isset ( $_SERVER ['HTTP_CONTENT_DISPOSITION'] ) && preg_match ( '/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i', $_SERVER ['HTTP_CONTENT_DISPOSITION'], $info )) { //HTML5上传
			file_put_contents ( $tempPath, file_get_contents ( "php://input" ) );
			$localName = $info [2];
		} else { //标准表单式上传
			$upfile = @$_FILES [$inputName];
			if (! isset ( $upfile ))
				$err = '文件域的name错误';
			elseif (! empty ( $upfile ['error'] )) {
				switch ($upfile ['error']) {
					case '1' :
						$err = '文件大小超过了php.ini定义的upload_max_filesize值';
						break;
					case '2' :
						$err = '文件大小超过了HTML定义的MAX_FILE_SIZE值';
						break;
					case '3' :
						$err = '文件上传不完全';
						break;
					case '4' :
						$err = '无文件上传';
						break;
					case '6' :
						$err = '缺少临时文件夹';
						break;
					case '7' :
						$err = '写文件失败';
						break;
					case '8' :
						$err = '上传被其它扩展中断';
						break;
					case '999' :
					default :
						$err = '无有效错误代码';
				}
			} elseif (empty ( $upfile ['tmp_name'] ) || $upfile ['tmp_name'] == 'none')
				$err = '无文件上传';
			else {
				move_uploaded_file ( $upfile ['tmp_name'], $tempPath );
				$localName = $upfile ['name'];
			}
		}
		
		if ($err == '') {
			$fileInfo = pathinfo ( $localName );
			$extension = $fileInfo ['extension'];
			if (preg_match ( '/' . str_replace ( ',', '|', $upExt ) . '/i', $extension )) {
				$bytes = filesize ( $tempPath );
				if ($bytes > $maxAttachSize)
					$err = '请不要上传大小超过' . formatBytes ( $maxAttachSize ) . '的文件';
				else {
					switch ($dirType) {
						case 1 :
							$attachSubDir = 'day_' . date ( 'ymd' );
							break;
						case 2 :
							$attachSubDir = 'month_' . date ( 'ym' );
							break;
						case 3 :
							$attachSubDir = 'ext_' . $extension;
							break;
					}
					$attachDir = $attachDir . '/' . $attachSubDir;
					if (! is_dir ( $attachDir )) {
						@mkdir ( $attachDir, 0777 );
						@fclose ( fopen ( $attachDir . '/index.htm', 'w' ) );
					}
					PHP_VERSION < '4.2.0' && mt_srand ( ( double ) microtime () * 1000000 );
					$newFilename = date ( "YmdHis" ) . mt_rand ( 1000, 9999 ) . '.' . $extension;
					$targetPath = $attachDir . '/' . $newFilename;
					
					rename ( $tempPath, $targetPath );
					@chmod ( $targetPath, 0755 );
					$targetPath = jsonString ( $targetPath );
					if ($immediate == '1')
						$targetPath = '!' . $targetPath;
					if ($msgType == 1)
						$msg = "'$targetPath'";
					else
						$msg = "{'url':'" . $targetPath . "','localname':'" . jsonString ( $localName ) . "','id':'1'}"; //id参数固定不变，仅供演示，实际项目中可以是数据库ID
				}
			} else
				$err = '上传文件扩展名必需为：' . $upExt;
			
			@unlink ( $tempPath );
		}
		
		echo "{'err':'" . jsonString ( $err ) . "','msg':" . $msg . "}";
		
		function jsonString($str) {
			return preg_replace ( "/([\\\\\/'])/", '\\\$1', $str );
		}
		function formatBytes($bytes) {
			if ($bytes >= 1073741824) {
				$bytes = round ( $bytes / 1073741824 * 100 ) / 100 . 'GB';
			} elseif ($bytes >= 1048576) {
				$bytes = round ( $bytes / 1048576 * 100 ) / 100 . 'MB';
			} elseif ($bytes >= 1024) {
				$bytes = round ( $bytes / 1024 * 100 ) / 100 . 'KB';
			} else {
				$bytes = $bytes . 'Bytes';
			}
			return $bytes;
		}
	}
	
	function show() {
		header ( 'Content-Type: text/html; charset=utf-8' );
		$magicQ = get_magic_quotes_gpc ();
		echo '<div>';
		for($i = 1; $i < 10; $i ++) {
			if (isset ( $_POST ['elm' . $i] )) {
				if ($magicQ)
					$_POST ['elm' . $i] = stripslashes ( $_POST ['elm' . $i] );
				echo '<textarea rows="10" cols="50">' . htmlspecialchars ( $_POST ['elm' . $i] ) . '</textarea>';
			}
		}
		echo '</div><p style="text-align:center;"><br /><input type="button" value="点击后退" onclick="javascript:history.back();" /></p>';
	}
	
		
		
		
	//-----------------------------------------------------	
	function pchat() {
		import ( "\pchat\pChart\pData.class" );
		import ( "\pchat\pChart\pChart.class" );
		
		// Dataset definition    
		$DataSet = new pData ();
		$DataSet->ImportFromCSV ( "Sample/bulkdata.csv", ",", array (1, 2, 3 ), FALSE, 0 );
		$DataSet->AddAllSeries ();
		$DataSet->SetAbsciseLabelSerie ();
		$DataSet->SetSerieName ( "January", "Serie1" );
		$DataSet->SetSerieName ( "February", "Serie2" );
		$DataSet->SetSerieName ( "March", "Serie3" );
		$DataSet->SetYAxisName ( "Average age" );
		$DataSet->SetYAxisUnit ( "祍" );
		
		// Initialise the graph   
		$Test = new pChart ( 700, 230 );
		$Test->setFontProperties ( "Fonts/tahoma.ttf", 8 );
		$Test->setGraphArea ( 70, 30, 680, 200 );
		$Test->drawFilledRoundedRectangle ( 7, 7, 693, 223, 5, 240, 240, 240 );
		$Test->drawRoundedRectangle ( 5, 5, 695, 225, 5, 230, 230, 230 );
		$Test->drawGraphArea ( 255, 255, 255, TRUE );
		$Test->drawScale ( $DataSet->GetData (), $DataSet->GetDataDescription (), SCALE_NORMAL, 150, 150, 150, TRUE, 0, 2 );
		$Test->drawGrid ( 4, TRUE, 230, 230, 230, 50 );
		
		// Draw the 0 line   
		$Test->setFontProperties ( "Fonts/tahoma.ttf", 6 );
		$Test->drawTreshold ( 0, 143, 55, 72, TRUE, TRUE );
		
		// Draw the line graph
		$Test->drawLineGraph ( $DataSet->GetData (), $DataSet->GetDataDescription () );
		$Test->drawPlotGraph ( $DataSet->GetData (), $DataSet->GetDataDescription (), 3, 2, 255, 255, 255 );
		
		// Finish the graph   
		$Test->setFontProperties ( "Fonts/tahoma.ttf", 8 );
		$Test->drawLegend ( 75, 35, $DataSet->GetDataDescription (), 255, 255, 255 );
		$Test->setFontProperties ( "Fonts/tahoma.ttf", 10 );
		$Test->drawTitle ( 60, 22, "example 1", 50, 50, 50, 585 );
		$Test->Render ( "example1.png" );
	}
	
	
	
	
}	